package com.mh.service;

import com.mh.dao.CustomerDAO;
import com.mh.dao.InvoiceDAO;

import com.mh.domain.Customer;
import com.mh.domain.Invoice;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Customer entities
 * 
 */

@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

	/**
	 * DAO injected by Spring that manages Customer entities
	 * 
	 */
	@Autowired
	private CustomerDAO customerDAO;

	/**
	 * DAO injected by Spring that manages Invoice entities
	 * 
	 */
	@Autowired
	private InvoiceDAO invoiceDAO;

	/**
	 * Instantiates a new CustomerServiceImpl.
	 *
	 */
	public CustomerServiceImpl() {
	}

	/**
	 * Return all Customer entity
	 * 
	 */
	@Transactional
	public List<Customer> findAllCustomers(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Customer>(customerDAO.findAllCustomers(startResult, maxRows));
	}

	/**
	 * Save an existing Customer entity
	 * 
	 */
	@Transactional
	public void saveCustomer(Customer customer) {
		Customer existingCustomer = customerDAO.findCustomerByPrimaryKey(customer.getId());

		if (existingCustomer != null) {
			if (existingCustomer != customer) {
				existingCustomer.setId(customer.getId());
				existingCustomer.setEmail(customer.getEmail());
				existingCustomer.setFirstName(customer.getFirstName());
				existingCustomer.setLastName(customer.getLastName());
				existingCustomer.setBirthday(customer.getBirthday());
				existingCustomer.setAddress(customer.getAddress());
				existingCustomer.setCity(customer.getCity());
				existingCustomer.setCreatedBy(customer.getCreatedBy());
				existingCustomer.setCreatedOn(customer.getCreatedOn());
				existingCustomer.setModifiedBy(customer.getModifiedBy());
				existingCustomer.setModifiedOn(customer.getModifiedOn());
			}
			customer = customerDAO.store(existingCustomer);
		} else {
			customer = customerDAO.store(customer);
		}
		customerDAO.flush();
	}

	/**
	 */
	@Transactional
	public Customer findCustomerByPrimaryKey(Integer id) {
		return customerDAO.findCustomerByPrimaryKey(id);
	}
	
	/**
	 */
	@Transactional
	public Set<Customer>  findCustomer(Customer customer) {
		return customerDAO.findCustomers(customer);
	}

	/**
	 * Return a count of all Customer entity
	 * 
	 */
	@Transactional
	public Integer countCustomers() {
		return ((Long) customerDAO.createQuerySingleResult("select count(o) from Customer o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing Customer entity
	 * 
	 */
	@Transactional
	public Set<Customer> loadCustomers() {
		return customerDAO.findAllCustomers();
	}

	/**
	 * Delete an existing Customer entity
	 * 
	 */
	@Transactional
	public void deleteCustomer(Customer customer) {
		customerDAO.remove(customer);
		customerDAO.flush();
	}

	/**
	 * Save an existing Invoice entity
	 * 
	 */
	@Transactional
	public Customer saveCustomerInvoices(Integer id, Invoice related_invoices) {
		Customer customer = customerDAO.findCustomerByPrimaryKey(id, -1, -1);
		Invoice existinginvoices = invoiceDAO.findInvoiceByPrimaryKey(related_invoices.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginvoices != null) {
			existinginvoices.setId(related_invoices.getId());
			existinginvoices.setDocument(related_invoices.getDocument());
			existinginvoices.setTotal(related_invoices.getTotal());
			existinginvoices.setCreatedBy(related_invoices.getCreatedBy());
			existinginvoices.setCreatedOn(related_invoices.getCreatedOn());
			existinginvoices.setModifiedBy(related_invoices.getModifiedBy());
			existinginvoices.setModifiedOn(related_invoices.getModifiedOn());
			related_invoices = existinginvoices;
		} else {
			related_invoices = invoiceDAO.store(related_invoices);
			invoiceDAO.flush();
		}

		related_invoices.setCustomer(customer);
		customer.getInvoices().add(related_invoices);
		related_invoices = invoiceDAO.store(related_invoices);
		invoiceDAO.flush();

		customer = customerDAO.store(customer);
		customerDAO.flush();

		return customer;
	}

	/**
	 * Delete an existing Invoice entity
	 * 
	 */
	@Transactional
	public Customer deleteCustomerInvoices(Integer customer_id, Integer related_invoices_id) {
		Invoice related_invoices = invoiceDAO.findInvoiceByPrimaryKey(related_invoices_id, -1, -1);

		Customer customer = customerDAO.findCustomerByPrimaryKey(customer_id, -1, -1);

		related_invoices.setCustomer(null);
		customer.getInvoices().remove(related_invoices);

		invoiceDAO.remove(related_invoices);
		invoiceDAO.flush();

		return customer;
	}
}
